/*
 * 文件名：EventLogInterceptor.java
 * 版权：MoonBox 
 * 修改人：Peter
 * 修改时间：2018年9月30日
 * 修改内容：新增
 */
package com.renzhebiao.lkang.Interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.renzhebiao.lkang.request.RequestCache;
import com.renzhebiao.lkang.request.RequestPayload;

import lombok.extern.log4j.Log4j;

/**
 * <p>
 * 日志拦截器
 * 
 * @author Peter
 * @since 1.0.0
 */
@Log4j
public class EventLogInterceptor extends HandlerInterceptorAdapter {

    public static final String  LOGGER_RETURN    = "_logger_return";

    // 请求开始时间标识
    private static final String LOGGER_SEND_TIME = "_send_time";
    // 请求日志实体标识
    private static final String LOGGER_ENTITY    = "_logger_entity";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object handler) throws Exception {
        RequestPayload req = RequestCache.get();
        if (req != null) {
            log.info("请求URL: " + req.getUrl());
            log.info("请求Body :" + req.getReqBody());
            log.info("请求头参数 : platformType ->" + req.getReqBody() + ",deviceType -> " + req.getDeviceType()
                     + ",deviceNo ->" + req.getDeviceNo() + ",userAgent->" + req.getUserAgent() + ",platformCode->"
                     + req.getPlatformCode());
        }
        // int a = 10 / 0;
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
                                Exception ex) throws Exception {
        RequestPayload payload = RequestCache.get();
        if (payload == null) {
            super.afterCompletion(request, response, handler, ex);
            return;
        }
        final Long startTime = payload.getStartTime();
        final Long endTime = System.currentTimeMillis();
        // if (!path.contains("/swagger") && !path.contains("/unauthorized") && !path.contains("/forbidden")) {
        if (!(handler instanceof HandlerMethod)) {
            log.error("............handler：" + handler.getClass().getName());
        }
        HandlerMethod handlerMethod = (HandlerMethod) handler;

        payload.setClassName(handlerMethod.getClass().getName() + "." + handlerMethod.getMethod().getName());
        payload.setDuration(endTime - startTime);
        payload.setHttpCode(response.getStatus());

        log.info("返回参数：" + payload.getResBody());
        log.info("本次请求耗时: {" + payload.getDuration() + "}毫秒; URI: {" + payload.getUri() + "}; ");
        log.info(payload.getHttpCode());

        super.afterCompletion(request, response, handler, ex);
    }

    /**
     * 判断是否为ajax请求
     * 
     * @param request
     * @return
     */
    public static String getRequestType(HttpServletRequest request) {
        return request.getHeader("X-Requested-With");
    }

}
